/*-----------------------------------------------------------------
   Project    : PlaygroundJ2eeBrasil
   Package    : com.wsc.j2eebrasil.bovespa.model.dao.text
   File name  : IndiceBovespaDAOTextImpl.java
   Author     : Wilson Soethe Cursino            Date: Apr 30, 2008
   Description:    
   Notes:   
   Revision History:
   Name:           Date:        Description:
  -----------------------------------------------------------------*/
package com.wsc.playground.j2ee.brasil.superbolsa.model.dao.text;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;

import org.apache.log4j.Logger;

import com.wsc.playground.j2ee.brasil.superbolsa.bean.IndicesBean;
import com.wsc.playground.j2ee.brasil.superbolsa.model.dao.IndiceBovespaDAO;

public class IndiceBovespaDAOTextImpl implements IndiceBovespaDAO {
    private final static ArrayList<IndicesBean> indices;

    private final static Logger LOG = Logger.getLogger(IndiceBovespaDAOTextImpl.class);

    static {
        IndicesBean indicesBean;
        ArrayList<IndicesBean> tempIndices = new ArrayList<IndicesBean>();
    	try {
            BufferedReader in = new BufferedReader(new FileReader("src/main/resources/ibovespa.txt"));
            String str = in.readLine(); //propositalmente vamos ignorar a primeira linha
            while ((str = in.readLine()) != null) {
                indicesBean = new IndicesBean();
                indicesBean.setCodigo(str.substring(0,10).trim());
                indicesBean.setAcao(str.substring(11,29).trim());
                indicesBean.setTipo(str.substring(29,47).trim());
                indicesBean.setQuantidade(new BigDecimal(str.substring(47,67).trim().replace(',','.')));
                indicesBean.setParticipacao(new BigDecimal(str.substring(67,str.length()).trim().replace(',','.')));
                tempIndices.add(indicesBean);            	
            }
            in.close();
        } catch (IOException e) {
            LOG.error("Erro ao tentar ler o ibovespa.txt");
        }
        indices = tempIndices;        
    } 

    public ArrayList<IndicesBean> getIndices() {
        return indices;
    }

}
